#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5005;
int q,pow3[N],a[N][N];
int get(int x,int id){
	for(int i = 0;;i++)if(x*(1ll<<i)+(1ll<<i)-1>id)return i-1;
}int cnt3[35];
signed main(){
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> q;
	for(int i = 1;i <= 5000;i++){
		for(int j = 1;j <= 5000;j++){
			a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+((i&j)==j);
		}
	}
	for(int i = 1,l,r;i <= q;i++){
		cin >> l >> r;
		cout << a[r][r]-a[r][l-1]-a[l-1][r]+a[l-1][l-1] << "\n";
	}
	return 0;
}

